﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
   public  class SachDAO
    {

        public object LayDSSach()
        {
            QLThuVienDataContext db = new QLThuVienDataContext();
            var ds = (from a in db.SACHes select new { a.MA_SACH, a.TEN_SACH, a.THE_LOAI, a.TAC_GIA, a.TINH_TRANG, a.NAMXB , a.NHA_XB, a.NGAY_NHAP, a.GIA_SACH, a.NGUOI_TIEP_NHAN }).AsQueryable();

            return ds;
        }

        public object LayLoaiSach()
        {
            QLThuVienDataContext db = new QLThuVienDataContext();
            return db.LOAISACHes;
        }

        public string ThemSach(SACH s)
        {
            QLThuVienDataContext db = new QLThuVienDataContext();

            db.SACHes.InsertOnSubmit(s);
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "";
        }

        public string ChinhSuaThongTinSach(SACH s)
        {
            QLThuVienDataContext db = new QLThuVienDataContext();
            var si = (from sa in db.SACHes where sa.MA_SACH == s.MA_SACH select sa).SingleOrDefault();

            if (si == null)
            {
                return "Cập nhật thất bại.";
            }

            si.TEN_SACH = s.TEN_SACH;
            si.THE_LOAI = s.THE_LOAI;
            si.TAC_GIA = s.TAC_GIA;
            si.TINH_TRANG = s.TINH_TRANG;
            si.NAMXB = s.NAMXB;
            si.NHA_XB = s.NHA_XB;
            si.NGAY_NHAP = s.NGAY_NHAP;
            si.GIA_SACH = s.GIA_SACH;
            si.NGUOI_TIEP_NHAN = s.NGUOI_TIEP_NHAN;
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "";
        }

        public string XoaSach(SACH s)
        {
            QLThuVienDataContext db = new QLThuVienDataContext();
            var getData = (from si in db.SACHes
                           where si.MA_SACH == s.MA_SACH
                           select si).Single();

            db.SACHes.DeleteOnSubmit(getData);
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "";
        }

        public string ThemLoaiSach(string tenloai)
        {
            QLThuVienDataContext db = new QLThuVienDataContext();
            LOAISACH ls = new LOAISACH();
            ls.TEN_LOAI = tenloai;
            db.LOAISACHes.InsertOnSubmit(ls);
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "";
        }

        public string XoaLoaiSach(int p)
        {
            QLThuVienDataContext db = new QLThuVienDataContext();
            var getData = (from ls in db.LOAISACHes
                           where ls.MA_LOAI == p
                           select ls).Single();

            db.LOAISACHes.DeleteOnSubmit(getData);
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "";
        }
    }
}
